package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
@Api(tags = "用户管理")
//本类控制类是用来接受请求 和返回响应的 所以不做业务方面的处理
//比如登录的时候要比对密码和用户名是否正确，这样的代码应该放在service（业务层）层中处理
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("login")
    @ApiOperation("用户登录")
    public JsonResult login(UserLoginParam userLoginParam){
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }

    @PostMapping("save")
    public JsonResult saveUser(UserSaveParam userSaveParam){
        log.debug("保存用户,userSaveParam={}",userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("select")
    public JsonResult selectUser(UserQuery userQuery){
        List<UserVO> list = userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("/update/password/{userId}")
    public JsonResult resetPassword(@PathVariable Long userId){
        log.debug("重置密码，userId={}",userId);
        userService.resetPassword(userId);
        return JsonResult.ok();
    }

    @PostMapping("update/status/{userId}/{status}")
    public JsonResult updateStatus(@PathVariable Long userId,@PathVariable String status){
        log.debug("修改用户状态，userId={},status={}",userId,status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }

    @PostMapping("/delete/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId){
        log.debug("删除用户，userId={}",userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }
    //查询上级审批人的信息
    @GetMapping("/select/audit/{parentId}")
    public JsonResult selectAuditList(@PathVariable  Long parentId){
       List<UserVO> auditList = userService.selectAuditList(parentId);
       return JsonResult.ok(auditList);
    }
}
